import Vue from 'vue'
import VueRouter from 'vue-router'
// 登陆页面
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// 后台主页
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
// 后台主页 Welcome
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')
// 用户列表组件
// import Users from '../components/Users.vue'
const Users = () => import(/* webpackChunkName:"user" */ '../components/Users.vue')
// 权限页面
// import Rights from '../components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName:"power" */ '../components/power/Rights.vue')
// 角色页面
// import Roles from '../components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName:"power" */ '../components/power/Roles.vue')
// 商品页面
// import Cate from '../components/goods/Cate.vue'
const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')
// 分类参数
// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')
// 商品列表
// import GoodsList from '../components/goods/GoodList.vue'
const GoodsList = () => import(/* webpackChunkName:"goods" */ '../components/goods/GoodList.vue')
// 商品列表 里 添加商品
// import Add from '../components/goods/Add.vue'
const Add = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')
// 订单管理
// import Order from '../components/order/Order.vue'
const Order = () => import(/* webpackChunkName:"order" */ '../components/order/Order.vue')
// 图片报表
// import Report from '../components/report/Report.vue'
const Report = () => import(/* webpackChunkName:"report" */ '../components/report/Report.vue')

Vue.use(VueRouter)
// 路由规则
const router = new VueRouter({
  routes: [
    // 路由重定向
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: GoodsList },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转来
  /// next 是一个函数，表示放行
  // 要去的路径
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
